package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.pojo.vo.WorkOrderTypeNodeVo;
import com.egoo.ticket.server.service.WorkOrderTypeNodeService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author：victor_tang
 * @Date ：2020/3/19
 * @Description：工单类型节点控制器
 */
@RestController
@RequestMapping(value = "/v1/workOrderTypeNode", produces = "application/json")
@Api(value = "工单类型节点管理",tags = "工单类型节点管理",description = "工单类型节点管理")
@Slf4j
public class WorkOrderTypeNodeController {

	@Autowired
	private WorkOrderTypeNodeService workOrderTypeNodeService;

	@ApiOperation(value = "根据工单类型节点id获取节点信息", notes = "根据工单类型节点id获取节点信息")
	@RequestMapping(method = RequestMethod.GET, value = "/getNodeInfoByNodeId")
	public DbResponse getNodeInfoByNodeId(@RequestParam(name = "workOrderTypeNodeId",
			required = false) Long workOrderTypeNodeId, HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(tenantId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "租户id不能为空！");
		}
		if (workOrderTypeNodeId == null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "工单类型节点id不能为空！");
		}
		try {
			WorkOrderTypeNodeVo vo = workOrderTypeNodeService.getNodeInfoByWorkOrderTypeId(workOrderTypeNodeId, tenantId);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, ResponseConstant.SELECT_SUCCESS_MSG, vo);
		} catch (Exception e) {
			log.error("获取工单类型节点详情异常：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"获取工单类型节点详情异常：" + e);
		}


	}
	@ApiOperation(value = "根据工单类型节点id获取节点处理人信息", notes = "根据工单类型节点id获取节点处理人信息")
	@RequestMapping(method = RequestMethod.GET, value = "/getNodeHandleUserInfoByNodeId")
	public DbResponse getNodeHandleUserInfoByNodeId(@RequestParam(name = "workOrderTypeNodeId",
			required = false) Long workOrderTypeNodeId, HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		if (StringUtils.isEmpty(tenantId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "租户id不能为空！");
		}
		if (workOrderTypeNodeId == null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"工单类型节点id不能为空！");
		}
		try {
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, ResponseConstant.SELECT_SUCCESS_MSG,
					workOrderTypeNodeService.getNodeHandleUserInfoByWorkOrderTypeId(workOrderTypeNodeId, tenantId,userId));
		} catch (Exception e) {
			log.error("获取工单类型节点处理人信息异常：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"获取工单类型节点处理人信息异常：" + e);
		}
	}

	@ApiOperation(value = "获取初始节点配置的问题分类数据", notes = "获取初始节点配置的问题分类数据")
	@RequestMapping(method = RequestMethod.GET, value = "/getTemplateFromOfStartNode")
	public DbResponse getTemplateFromOfStartNode(@RequestParam("workOrderTypeId")Long workOrderTypeId) {
		if (workOrderTypeId == null){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"流程id不能为空");
		}
		return workOrderTypeNodeService.getTemplateFromOfStartNode(workOrderTypeId);
	}
}
